/**
 * 添加或者修改页面
 */
var TplsInfoDlg = {
    data: {
        id: "",
        fwid: "",
        flid: "",
        name: "",
        pics: "",
        mainPic: "",
        detail: "",
        ms: "",
        jiage: "",
        backColor: "",
        sy: "",
        sizeStr: "",
        tags: "",
        zuozhe: "",
        createTime: "",
        del: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects', 'tagsInput'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var tagsInput = layui.tagsInput;


    $('#tags').tagsInput({removeWithBackspace: true});




  //实例化编辑器
    var ue = UE.getEditor('detail', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/ueditor/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };
    
    
    function getMainColor(img){
    	console.log(img)
    	var _this = this;
        RGBaster.colors(img, {
            paletteSize: 10,  //调色板大小（可选项）
            exclude: ['rgb(0,0,0)', 'rgb(255,255,255)' ,'rgb(254,254,254)' ],//剔除的颜色
            ocp:'0.1',
            success: function(payload) {
                // payload.dominant是主色，RGB形式表示
                // payload.secondary是次色，RGB形式表示
                // payload.palette是调色板，含多个主要颜色，数组
                // console.log(payload.dominant);
                // console.log(payload.secondary);
                // console.log(payload.palette);
            	$("#backColor").val(payload.dominant)
                console.log(payload)
            }
        });
    };





    //普通图片上传
    var upload = layui.upload;
    upload.render({
        elem: '#mainPicBtn'
        ,accept: 'images'
        ,url: Feng.ctxPath + '/system/uploadoss' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
            	getMainColor(result)
            });
        }
        ,done: function(res){
        	Feng.success("上传成功！");
        	var url = res.data.url
        	
        	$("#mainPicadd").attr("style","display:none;")
        	$("#mainPicimg").attr("src",url)
        	$("#mainPic").val(url)
        	$("#mainPicimg").attr("style","display:block;")
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });



    //普通图片上传
    var upload2 = layui.upload;
    upload2.render({
        elem: '#picsBtn'
        ,accept: 'file'
        ,url: Feng.ctxPath + '/system/uploadoss' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
            	var size = file.size
            	$("#sizeStr").val(sizeTostr(size))
            });
        }
        ,done: function(res){
        	Feng.success("上传成功！");
        	var url = res.data.url
        	$("#picsadd").attr("style","display:none;")
        	$("#pics").val(url)
        	$("#picsimg").attr("style","display:block;")
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });



    function sizeTostr(size) {
        var data = "";
        if (size < 0.1 * 1024) { //如果小于0.1KB转化成B  
            data = size.toFixed(2) + "B";
        } else if (size < 0.1 * 1024 * 1024) {//如果小于0.1MB转化成KB  
            data = (size / 1024).toFixed(2) + "KB";
        } else if (size < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB  
            data = (size / (1024 * 1024)).toFixed(2) + "MB";
        } else { //其他转化成GB  
            data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }
        var sizestr = data + "";
        var len = sizestr.indexOf("\.");
        var dec = sizestr.substr(len + 1, 2);
        if (dec == "00") {//当小数点后为00时 去掉小数部分  
            return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
        }
        return sizestr;
    }



























    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tpls/addItem", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


});